package Dao;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Vao.User;
import util.JDBCUtil;

import javax.servlet.jsp.jstl.sql.Result;

public class UserDao {
    public int checkId(String account,String password) throws Exception{
        int id=0;
        Connection conn = new JDBCUtil().getCon();
        String sql="select userId from user where account=? and password=?;";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, account);
        ps.setString(2, password);
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            id=rs.getInt(1);
        }
        rs.close();
        ps.close();
        conn.close();
        return id;
    }

    public int insertUser(String account,String password1) throws SQLException {
        Connection conn = new JDBCUtil().getCon();
        String sql="INSERT INTO user(account,password) values(?,?)";
        PreparedStatement ps =conn.prepareStatement(sql);
        ps.setString(1,account);
        ps.setString(2,password1);
        int i=ps.executeUpdate();
        conn.close();
        ps.close();
        return i;
    }

    public ResultSet login(String account,String password) throws SQLException {
        Connection conn = new JDBCUtil().getCon();
        String sql="select userId from user where account=? and password=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, account);
        ps.setString(2, password);
        ResultSet rs=ps.executeQuery();
        conn.close();
        return rs;
    }
}
